<?php
class Stock_barang_model extends CI_Model{
    
	function   __construct() {
        parent::__construct();
    }
	
	//-------------------- Penghitungan stock barang detik itu juga ---------------------//	
	function get_kartu_stock($cabang)
	{
		$query = $this->db->query("SELECT stok_barang.intid_barang, 
		strnama, 
		IF(set_fisik IS NULL, 0, set_fisik) AS set_fisik, 
		IF(pcs_fisik IS NULL, 0, pcs_fisik) AS pcs_fisik, 
		IF(set_hutang IS NULL, 0, set_hutang) AS set_hutang, 
		IF(pcs_hutang IS NULL, 0, pcs_hutang) AS pcs_hutang, 
		DATE((SELECT as_of_date FROM stok_barang WHERE intid_cabang = '$cabang' LIMIT 0,1)) AS as_of_date, intquantity, datetgl 
		FROM 
			(SELECT barang.intid_barang, strnama, set_fisik, pcs_fisik, set_hutang, pcs_hutang, as_of_date 
				FROM barang 
				LEFT JOIN 
					(SELECT * 
						FROM stok_barang WHERE intid_cabang = '$cabang') 
					AS stok_barang 
				ON barang.intid_barang = stok_barang.intid_barang ORDER BY intid_barang) 
			AS stok_barang 
		LEFT JOIN 
			(SELECT intid_barang, SUM(intquantity) AS intquantity, datetgl 
				FROM 
					(SELECT intid_barang, intquantity, datetgl 
						FROM nota INNER JOIN nota_detail ON nota.intid_nota = nota_detail.intid_nota 
						WHERE datetgl > (SELECT as_of_date FROM stok_barang WHERE intid_cabang = '$cabang' LIMIT 0,1) AND intid_cabang = '$cabang') 
					AS nota GROUP BY intid_barang) 
			AS nota
		ON stok_barang.intid_barang = nota.intid_barang 
		WHERE intquantity IS NOT NULL OR set_fisik IS NOT NULL ORDER BY strnama");
        return $query->result();
	}
	function delete_stock($cabang)
	{
		$this->db->where('intid_cabang', $cabang);
		$this->db->delete('stok_barang');
	}
	function stock_insert($data)
	{
		$this->db->insert('stok_barang', $data);
	}
	function date_update($cabang, $year, $month, $day, $time){
		$date = $year . "-" . $month . "-" . $day . " " . $time;
		$data = array(
			'as_of_date'=>$date,
			);
		$this->db->where('intid_cabang',$cabang);
		$this->db->update('stok_barang',$data);
	}

}
?>
